package users

import (
	"github.com/gin-gonic/gin"
	"go_auth/ext/app"
	"go_auth/ext/e"
	"go_auth/ext/settings"
	"go_auth/servers/users"
	"go_auth/utils/commons"
	"net/http"
)

func Role(c *gin.Context) {
	roleId := c.PostForm("roleId")
	opt := c.PostForm("type")
	roleName := c.PostForm("roleName")
	var appG = app.Gin{C: c}
	code := e.Success

	role := users.Role{}
	role.RoleName = roleName

	if opt == settings.Add {
		if role.AddRole() == settings.Zero {
			code = e.AddError
		}
	} else if opt == settings.Delete {
		i, f := commons.StrToInt(roleId)
		if !f {
			appG.Response(http.StatusOK, e.AtoB, roleId)
			return
		}
		role.RoleId = i
		if role.DeleteRole() == settings.Zero {
			code = e.DeleteError
		}
	} else if opt == settings.Update {
		if role.UpdateRole() == settings.Zero {
			code = e.UpdateError
		}
	} else {
		code = e.ParamsError
	}

	appG.Response(http.StatusOK, code, roleId)
}

func FindRoles(c *gin.Context) {
	role := users.Role{}
	roles := role.FindRoles()
	var appG = app.Gin{C: c}
	code := e.Success

	if roles == nil {
		code = e.EmptyError
	}

	appG.Response(http.StatusOK, code, roles)
}
